//
//  APEdgePanScrollView.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/8/22.
//

/**
 ## 实战
 在使用了第三方库`JXPagingView`时，在某些特殊情况需要自己处理`UIScrollView`内部逻辑。
 比如项目用了`FDFullscreenPopGesture`，需要处理手势相关代理。
 示例：
 ```
 extension YourVC: JXSegmentedViewDelegate {
     func scrollViewClass(in listContainerView: JXSegmentedListContainerView) -> AnyClass {
         return APEdgePanScrollView.self
     }
 }
 ```
 
 ### 组队（甜心叫早、心动连线、游戏开黑）业务实战
 > 备注：打印结果，小数点后超过2位的均已手动移除，便于查看
 
 /// ①：顶部（甜心叫早描述区域）侧滑，不返回
 point is (3.0, 0.0), location is (20.0, 119.66), contentOffset is (0.0, 0.0)
 
 /// ②：第三个cell侧滑，不返回
 point is (0.33, 0.0), location is (21.33, 530.33), contentOffset is (0.0, 0.0)
 
 /// ③：（手动滑动）甜心叫早 → 心动连线
 point is (-8.66, 0.0), location is (316.0, 552.66), contentOffset is (0.0, 0.0)
 
 /// ④：（手动滑动）心动连线 → 游戏开黑
 point is (-10.66, 0.0), location is (738.66, 572.33), contentOffset is (414.0, 0.0)
 
 /// ⑤：（手动滑动）心动连线 ← 游戏开黑
 point is (0.66, 0.0), location is (1010.33, 578.33), contentOffset is (828.0, 0.0)
 
 /// ⑥：（手动滑动）甜心叫早 ← 心动连线
 point is (6.0, 0.0), location is (611.0, 591.33), contentOffset is (414.0, 0.0)
 
 /// ⑦：侧滑，返回
 point is (15.33, 0.0), location is (35.33, 489.33), contentOffset is (0.0, 0.0)

 ## 技术支持
 > - `location(in view:` 是 `UIGestureRecognizer` 中的方法，获取到的是**手指点击屏幕实时的坐标点**
 > - `translation(in view:` 是 `UIPanGestureRecognizer` 中的方法，获取到的是**手指移动后，在相对坐标中的偏移量**
 */

import UIKit

/// 自定义`UIScrollView`类，多用于处理侧滑返回手势冲突
open class APEdgePanScrollView: UIScrollView {
    
    /// 侧滑（从左往右）返回时，左边缘最大可识别手势的范围，默认160
    open var maxEdgeLocationX: CGFloat = 160
    
    /// 是否开启debug打印log
    open var debugLogEnable: Bool = false
    
    /// 是否正在侧滑返回
    fileprivate var isLeftPanBacking: Bool = false
    
    /// 当前类（子类）重写 `UIView (UIViewGestureRecognizers)` 中的 `gestureRecognizerShouldBegin` 方法
    open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        /// 识别手势为Pan类型手势
        guard let panG = gestureRecognizer as? UIPanGestureRecognizer else { return false }
        if panG.state == .possible || panG.state == .began {
            let point = panG.translation(in: self)
            let location = panG.location(in: self)
            #if DEBUG
            if debugLogEnable {
                print("APEdgePanScrollView侧滑 point is \(point), location is \(location), contentOffset is \(contentOffset)")
            }
            #endif
            if point.x > 0, location.x < maxEdgeLocationX, contentOffset.x <= 0 {
                isLeftPanBacking = true
                return true
            }
        }
        return true
    }
    
}

extension APEdgePanScrollView: UIGestureRecognizerDelegate {
    
    /**
     Simultaneously: adv. 同时地
     */
    
    /// 是否允许多个手势识别器同时地识别
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if isLeftPanBacking {
            /// 如果正在侧滑返回，允许同时识别多个手势，这也是改
            isLeftPanBacking = false
            return true
        } else {
            return false
        }
    }
    
}
